package com.aoyy.controller;

import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.springframework.stereotype.Controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import com.aoyy.service.UserService;
import com.aoyy.model.User;
import com.aoyy.constant.Constants;
/**===============================================================================================================
 * User controller <code>UserController</code>, its goals are to sign in, sign out, sign up, and so on.  
 * @author this author
 * @date 2017-08-09
 * ================================================================================================================ */
@Controller
public class UserController extends BaseController {
	@Autowired
	private UserService userService;
	
	// To sign in
    @RequestMapping(value="/user/signIn", method={RequestMethod.GET}) 
    public String toSignIn() {
    	return "/user/signIn";
    }
    
    // Sign in
    @RequestMapping(value="/user/signIn", method={RequestMethod.POST})
    public ModelAndView signIn(HttpServletRequest request, User user) {
    	ModelAndView view = new ModelAndView();
    	view.setViewName("/user/signIn");
    	User user2 = userService.queryUserByUserName(user.getUserName());
    	if (user2 == null) {
    		view.addObject("message", "The User Name doesn't exist!");
    		return view;
    	} 
    	if (!user2.getPassword().equals(user.getPassword())) {
    		view.addObject("message", "The Password is incorrect.");
    		return view;
    	}
    	if (user2.getLocked() == Constants.USER_LOCKED) {
    		view.addObject("message", "The User has been locked, and you cann\'t sign in!");
    		return view;
    	} 
    	user2.setLastIp(request.getRemoteAddr());
    	user2.setLastVisit(new Date());
    	userService.updateAfterSignIn(user2);
    	setSessionUser(request, user2);
        view.setViewName("redirect:/index");
    	return view;
	}
	
    // To sign up
    @RequestMapping(value="/user/signUp", method={RequestMethod.GET})
    public String toSignUp() {
    	return "/user/signUp";
    }
    
    // Sign up
	@RequestMapping(value="/user/signUp", method={RequestMethod.POST})	
    public ModelAndView signUp(HttpServletRequest request, User user, String passwordAgain) {
		ModelAndView view = new ModelAndView();
		view.setViewName("/user/signUp");
		if (user.getUserName() == null || user.getUserName().isEmpty() || user.getUserName().length() > 45) {
			view.addObject("message", "The User Name is required and no more than 45 characters.");
			return view;
		}
		if (user.getPassword() == null || user.getPassword().isEmpty()) {
			view.addObject("message", "The Password is required.");
			return view;
		}
		if (user.getPassword().length() < 6 || user.getPassword().length() > 15) {
			view.addObject("message", "The Password is required to be between 6 and 15 characters.");
			return view;
		}
		if (passwordAgain == null || passwordAgain.isEmpty()) {
			view.addObject("message", "The Password Again is required.");
			return view;
		}
		if (!passwordAgain.equals(user.getPassword())) {
			view.addObject("message", "The Password Again is inconsistent width Password.");
			return view;
		}
		User user2 = userService.queryUserByUserName(user.getUserName());
		if (user2 != null) {
			view.addObject("message", "The User Name has been used to register!");
			return view;
		}
		user.setUserType(Constants.USER_TYPE_GENERAL_USER);
		user.setCredit(Constants.SIGNUP_CREDIT);
		user.setLocked(Constants.USER_UNLOCKED);
		userService.save(user);
		view.addObject("message", "User Sign Up Success!");
		return view;
	}
	
	// Sign out
	@RequestMapping(value="/user/signOut")
	public String signOut(HttpSession session) {
    	session.removeAttribute(Constants.SESSION_USER);
     	return "redirect:/index";
	}
	
	@RequestMapping(value="/user/lockOrUnlockUser", method={RequestMethod.GET}) 
	public String toLockOrUnlockUser(HttpServletRequest request) {
		List<User> userList = userService.queryAllUsers();
		request.setAttribute("userList", userList);
		return "/user/lockOrUnlockUser";
	}
	
	// Lock/Unlock user 
	@RequestMapping(value="/user/lockOrUnlockUser", method={RequestMethod.POST}) 
	public ModelAndView lockOrUnlockUser(User user) {
		ModelAndView view = new ModelAndView();
		view.setViewName("/user/lockOrUnlockUser");
		User user2 = userService.queryUserByUserName(user.getUserName()); 
		if (user2 == null) {
			view.addObject("message", "The User doesn\'t exist!");
		} else {
			if (user.getLocked() == 0) {
				userService.unlockUser(user.getUserName());
				view.addObject("message", "Unlock User Success!");
			} else {
				userService.lockUser(user.getUserName());
				view.addObject("message", "Lock User Success!");
			}
		}
		List<User> userList = userService.queryAllUsers();
		view.addObject("userList", userList);
		return view;
	}
	
}
